package zjhr.com.controller;

import java.io.IOException;
import java.util.List;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import zjhr.com.pojo.EasyUIPageQuery;
import zjhr.com.pojo.ZjInmoney;
import zjhr.com.service.InMoneyService;
import zjhr.com.utils.FileUtils;
import zjhr.com.utils.JsonUtils;


@Controller
public class InMoneyController {
	
	@Autowired
	private InMoneyService inMonService;
	//添加进账信息
	@RequestMapping("/addInMoneyInfo.action")
	public String addInMoney(ZjInmoney inmoney){
		inMonService.save(inmoney);
		return "/caiwu/inMoney.jsp";
	}
	//修改进账信息
	@RequestMapping("/editInMoneyInfo.action")
	public String editInMoney(ZjInmoney inmoney){
		inMonService.edit(inmoney);
		return "/caiwu/inMoney.jsp";
	}
	
	//分页
	@RequestMapping("/InMoneyPageQuery.action")
	@ResponseBody
	public String pageQuery(Integer page,Integer rows){
		
		EasyUIPageQuery pageQuery = inMonService.pageQuery(page, rows);
		
		return JsonUtils.objectToJson(pageQuery);
		
	}
	//删除进账信息
	@RequestMapping("/deleteInMoney.action")
	public String deleteInMoney(String ids){
		inMonService.delete(ids);
		return "/caiwu/inMoney.jsp";
	}
	
	//导出进账信息
	@RequestMapping("/exportInMoneyInfo.action")
	public void exportInMoney(String beginDate,String endDate,HttpServletRequest request,HttpServletResponse response) throws Exception{
		List<ZjInmoney> list = inMonService.findAll(beginDate,endDate);
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet hssfSheet = workbook.createSheet("进账数据");
		HSSFRow headRow = hssfSheet.createRow(0);
		headRow.createCell(0).setCellValue("编号");
		headRow.createCell(1).setCellValue("收款人");
		headRow.createCell(2).setCellValue("收款金额");
		headRow.createCell(3).setCellValue("收款时间");
		headRow.createCell(4).setCellValue("备注");
		for (ZjInmoney inmoney : list) {
			HSSFRow hssfRow = hssfSheet.createRow(hssfSheet.getLastRowNum()+1);
			hssfRow.createCell(0).setCellValue(hssfSheet.getLastRowNum());
			hssfRow.createCell(1).setCellValue(inmoney.getInname());
			hssfRow.createCell(2).setCellValue(inmoney.getInmoney());
			hssfRow.createCell(3).setCellValue(inmoney.getIndate());
			hssfRow.createCell(4).setCellValue(inmoney.getIncomment());
		}
		//第三步：使用输出流进行文件下载（一个流、两个头）
		String filename = "进账数据.xls";
		
		String contentType = new MimetypesFileTypeMap().getContentType(filename);//获取文件类型
		ServletOutputStream out = response.getOutputStream();
		response.setContentType(contentType);
		
		//获取客户端浏览器类型
		String agent = request.getHeader("User-Agent");
		filename = FileUtils.encodeDownloadFilename(filename, agent);
		response.setHeader("content-disposition", "attachment;filename="+filename);
		workbook.write(out);
		out.close();
		
	}

	

}
